package com.finiac.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap; 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;


import com.finiac.dao.ProductDAO;
import com.finiac.model.Product;
@Component
public class AddStockController extends MultiActionController {

	ProductDAO productDAO;
	
	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}
	
	public ModelAndView addStock(HttpServletRequest request, HttpServletResponse response, Product product)throws Exception
	{
		product.setIsActive(1);
		productDAO.addProduct(product);
		return new ModelAndView("redirect:list.htm");
	}
	
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("productList", productDAO.listProduct());
		modelMap.addAttribute("product", new Product());
		return new ModelAndView("addStock",modelMap);
	}
	
	public void deleteAddStock(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		Product product = new Product();
		List<Product> productList= productDAO.selectById(id);
		for(int i=0;i<productList.size();i++)
		{
			product = productList.get(i);
		}
		product.setIsActive(0);
		productDAO.updateProduct(product);
	}
	public ModelAndView updateStockPage(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("productList", productDAO.selectById(id));
		modelMap.addAttribute("product", new Product());
		return new ModelAndView("updateStock",modelMap);
	}
	
	
	public ModelAndView updateStock(HttpServletRequest request, HttpServletResponse response, Product product)throws Exception
	{
		float qtyPur=Float.parseFloat(request.getParameter("qtyPur"));
		float qtyAvail= product.getQtyAvail();
		product.setQtyAvail(qtyAvail+qtyPur);
		product.setIsActive(1);
		productDAO.updateProduct(product);
		return new ModelAndView("redirect:list.htm");
	}
	
	public ModelAndView stockStatus(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("productList", productDAO.listProduct());
		modelMap.addAttribute("product", new Product());
		return new ModelAndView("stockStatus",modelMap);
	}
	public ModelAndView setSellingPricePage(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("productList", productDAO.listProduct());
		modelMap.addAttribute("product", new Product());
		return new ModelAndView("setSellingPrice",modelMap);
	}
	public void setSellingPrice(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		Product product= new Product();
		int id=Integer.parseInt(request.getParameter("id")); 
		float sellingPrice= Float.parseFloat(request.getParameter("sellingPrice"));
		List<Product> productList= productDAO.selectById(id);
		for(int i=0;i<productList.size();i++)
		{
			product = productList.get(i);
		}
		product.setSellingPrice(sellingPrice);
		productDAO.updateProduct(product);
		
	}
}
